package com.hemaapp.yjnh.activity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.uu.view.ActionSheetDialog;
import com.android.uu.view.ActionSheetDialog.OnSheetItemClickListener;
import com.android.uu.view.ActionSheetDialog.SheetItemColor;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.dialog.HemaButtonDialog;
import com.hemaapp.hm_FrameWork.dialog.HemaButtonDialog.OnButtonListener;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseFragmentActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.TabViewPagerAdapter;
import com.hemaapp.yjnh.fragment.ChatListFragment;
import com.hemaapp.yjnh.fragment.NoticeListFragment;

/**消息通知列表
 * @author ZhangGuiyou
 * Date: 2015-07-30 11:37
 *
 */
public class NoticeListActivity extends BaseFragmentActivity {

	@Bind(R.id.title_left_btn)
	ImageButton titleLeftBtn;
	@Bind(R.id.title_right_btn)
	Button titleRightBtn;
    @Bind(R.id.title_right_img)
    ImageView titleRightImg;
	@Bind(R.id.title_text)
	TextView titleText;
	@Bind(R.id.layout_tab)
	TabLayout layoutTab;
	@Bind(R.id.viewpager)
	ViewPager viewpager;

	private ChatListFragment chatListFragment = new ChatListFragment();
    private NoticeListFragment replyFragment = new NoticeListFragment();
    private NoticeListFragment sysFragment = new NoticeListFragment();
    private TabViewPagerAdapter pagerAdapter;
    private int startPage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_custom_orders);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        initDatas();
	}
	
	private void initDatas() {
        List<String> titles = new ArrayList<>();
        titles.add("聊天消息");
        titles.add("评论回复");
        titles.add("系统通知");
        replyFragment.setNoticetype("2");
        sysFragment.setNoticetype("1");
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(chatListFragment);
        fragments.add(replyFragment);
        fragments.add(sysFragment);
        pagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewpager.setAdapter(pagerAdapter);
        viewpager.setOffscreenPageLimit(3);
        viewpager.setCurrentItem(startPage);
        layoutTab.setTabMode(TabLayout.MODE_FIXED);
        layoutTab.setupWithViewPager(viewpager);
        layoutTab.setTabsFromPagerAdapter(pagerAdapter);
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {

		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
	}

	@Override
	protected void findView() {
	}

	@Override
	protected void getExras() {
		startPage = mIntent.getIntExtra("start", 0);
	}

	@Override
	protected void setListener() {
		titleText.setText("消息通知");
		titleRightBtn.setVisibility(View.GONE);
        titleRightImg.setVisibility(View.VISIBLE);
        titleRightImg.setImageResource(R.drawable.notice_operate);
	}

    @OnClick({R.id.title_left_btn, R.id.title_right_img})
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_btn:
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.title_right_img://清空
			showNotice();
			break;
		}
	}

    @Override
	protected boolean onKeyBack() {
		setResult(RESULT_OK);
		finish();
		return false;
	}

	/**
	 * 显示图片选择对话
	 */
	public void showNotice() {
	    new ActionSheetDialog(mContext)
        .builder()
        .setCancelable(true)
        .setCanceledOnTouchOutside(true)
        .addSheetItem("清空", SheetItemColor.Black,
                new OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                    	HemaButtonDialog dialog = new HemaButtonDialog(mContext);
                        dialog.setText("确定清空通知列表?");
                        dialog.setLeftButtonText("取消");
                        dialog.setRightButtonText("确定");
                        dialog.setRightButtonTextColor(getResources().getColor(R.color.theme_orange));
                        dialog.setButtonListener(new OnButtonListener() {

                            @Override
                            public void onRightButtonClick(HemaButtonDialog dialog) {
                                if (viewpager.getCurrentItem() == 0)
                                    chatListFragment.clearAll();
                                else if (viewpager.getCurrentItem() == 1)
                                    replyFragment.clearAll();
                                else if (viewpager.getCurrentItem() == 2)
                                    sysFragment.clearAll();
                                dialog.cancel();
                            }

                            @Override
                            public void onLeftButtonClick(HemaButtonDialog dialog) {
                                dialog.cancel();
                            }
                        });
                    }
                })
        .addSheetItem("全部设为已读", SheetItemColor.Black,
                new OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                    	 HemaButtonDialog dialog = new HemaButtonDialog(mContext);
                         dialog.setText("确定将全部消息置为已读？");
                         dialog.setLeftButtonText("取消");
                         dialog.setRightButtonText("确定");
                         dialog.setRightButtonTextColor(getResources().getColor(R.color.theme_orange));
                         dialog.setButtonListener(new OnButtonListener() {

                             @Override
                             public void onRightButtonClick(HemaButtonDialog dialog) {
                                 dialog.cancel();
                                 if (viewpager.getCurrentItem() == 0)
                                     chatListFragment.readAll();
                                else if (viewpager.getCurrentItem() == 1)
                                     replyFragment.readAll();
                                else if (viewpager.getCurrentItem() == 2)
                                     sysFragment.readAll();
                             }

                             @Override
                             public void onLeftButtonClick(HemaButtonDialog dialog) {
                                 dialog.cancel();
                             }
                         });
                    }
                })
         .setCancelColor(getResources().getColor(R.color.text_dialog_cancel))
         .show();
	}
}
